html   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 9
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 9
rs 10
c 0
b 0
f 0
wmc 12

1 Function

Rating   Name   Duplication   Size   Complexity  
A create 0 7 1
1
/**
2
 * @param {createElementOpt} options
3
 */
4
function createElement(options) {
5
  var el, a, i;
6
  if (!options.tagName) {
7
    el = document.createDocumentFragment();
8
  } else {
9
    el = document.createElement(options.tagName);
10
    if (options.className) {
11
      el.className = options.className;
12
    }
13
14
    if (options.attributes) {
15
      for (a in options.attributes) {
16
        el.setAttribute(a, options.attributes[a]);
17
      }
18
    }
19
20
    if (options.html !== undefined) {
21
      el.innerHTML = options.html;
22
    }
23
  }
24
25
  if (options.text) {
26
    el.appendChild(document.createTextNode(options.text));
27
  }
28
29
  // IE 8 doesn"t have HTMLElement
30
  if (window.HTMLElement === undefined) {
31
    // @ts-ignore
32
    window.HTMLElement = Element;
33
  }
34
35
  if (options.childs && options.childs.length) {
36
    for (i = 0; i < options.childs.length; i++) {
37
      el.appendChild(
38
        options.childs[i] instanceof window.HTMLElement
39
          ? options.childs[i]
40
          : createElement(options.childs[i])
41
      );
42
    }
43
  }
44
45
  return el;
46
}
47
48
class html {
49
  static create(options) {
50
    /**
51
     * @param {createElementOpt}
52
     * @returns {createElement}
53
     */
54
    return createElement(options);
55
  }
56
}
57